package cn.agiledata.bank.transaction.dao;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.SessionFactory;

import cn.agiledata.bank.common.dao.HqlPageListTemplate;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.StringUtil;
import cn.agiledata.bank.transaction.logic.EsPrePayTransBean;
import com.vnv.infrastructure.logic.IPBAppFactory;

public class EsPrePayTransPageList extends HqlPageListTemplate {
    private EsPrePayTransBean bean;

    public EsPrePayTransPageList(EsPrePayTransBean bean) {
        this.setSessionFactory((SessionFactory) IPBAppFactory
                .getObj("sessionFactory"));
        this.bean = bean;
    }

    public String getHql() {
        String hql = "from EsPrePayTrans t where 1=1";
        if (!StringUtil.isStrEmpty(bean.getTransStatus())) {
            hql += " and t.transStatus = :transStatus";
        }
        if (bean.getAccount() != null) {
            hql += " and t.debit.account = :account";
        }
        if (bean.getSubAccounts() != null && bean.getSubAccounts().size() > 0) {
            hql += " and t.debit in (:subAccountList)";
        }
        if (!bean.isBatchType()) {
            hql += " and t.batchNo is null";
        } else if (StringUtil.isStrEmpty(bean.getBatchNo())) {
            hql += " and t.batchNo is not null";
        } else {
            hql += " and t.batchNo like :batchNo";
        }
        if (!StringUtil.isStrEmpty(bean.getTransId())) {
            hql += " and t.transId like :transId";
        }
        if (bean.getBeginDate() != null) {
            hql += " and t.beginDate >= :beginDate";
        }
        if (bean.getEndDate() != null) {
            hql += " and t.beginDate <= :endDate";
        }
        if (!StringUtil.isStrEmpty(bean.getAmountMax())) {
            hql += " and t.amount <= :amountMax";
        }
        if (!StringUtil.isStrEmpty(bean.getAmountMin())) {
            hql += " and t.amount >= :amountMin";
        }
        if (!StringUtil.isStrEmpty(bean.getActionTypeID())) {
            hql += " and t.actionType.id = :actionType";
        }
        else {
        	// zln 修正 2008-1-12 选择全部交易类型时对actiontype的限制
        	hql += " and t.actionType.id in (:actionTypes)";
        }
        
        if (!StringUtil.isStrEmpty(bean.getSubAccountID())) {
            hql += " and t.debit.id = :subAccountID";
        }
        if (!StringUtil.isStrEmpty(bean.getCreateUser())) {
            hql += " and (t.operatorSeq = :createUser or t.operatorSeq is null)";
        }
        hql += " order by t.updateTime desc";
        return hql.toString();
    }

    public String getPageRecord() {
        return Constant.PAGESIZE.toString();
    }

    public Map getParams() {
        Map map = new HashMap();
        if (!StringUtil.isStrEmpty(bean.getTransStatus())) {
            map.put("transStatus", bean.getTransStatus());
        }
        if (bean.getAccount() != null) {
            map.put("account", bean.getAccount());
        }
        if (bean.getSubAccounts() != null && bean.getSubAccounts().size() > 0) {
            map.put("subAccountList", bean.getSubAccounts());
        }
        if (bean.isBatchType() && !StringUtil.isStrEmpty(bean.getBatchNo())) {
            map.put("batchNo", "%" + bean.getBatchNo() + "%");
        }
        if (!StringUtil.isStrEmpty(bean.getTransId())) {
            map.put("transId", "%" + bean.getTransId() + "%");
        }
        if (bean.getBeginDate() != null) {
            map.put("beginDate", bean.getBeginDate());
        }
        if (bean.getEndDate() != null) {
            map.put("endDate", bean.getEndDate());
        }
        if (!StringUtil.isStrEmpty(bean.getAmountMax())) {
            map.put("amountMax", new Double(bean.getAmountMax()));
        }
        if (!StringUtil.isStrEmpty(bean.getAmountMin())) {
            map.put("amountMin", new Double(bean.getAmountMin()));
        }
        if (!StringUtil.isStrEmpty(bean.getActionTypeID())) {
            map.put("actionType", new Long(bean.getActionTypeID()));
        }
        else {
        	map.put("actionTypes", bean.getActionTypes());
        }
        if (!StringUtil.isStrEmpty(bean.getSubAccountID())) {
            map.put("subAccountID", new Long(bean.getSubAccountID()));
        }
        if (!StringUtil.isStrEmpty(bean.getCreateUser())) {
            map.put("createUser", bean.getCreateUser());
        }
        return map;
    }

    public String getStartPage() {
        // TODO Auto-generated method stub
        if (bean.getStartPage() != null)
            return bean.getStartPage();
        else
            return "1";
    }

}
